package com.jinmdz.fmis.api.api.service;

import com.jinmdz.fmis.api.api.model.area.AreaOptionItem;
import com.jinmdz.fmis.api.api.model.area.AreaParentData;
import com.jinmdz.fmis.api.base.BaseService;
import com.jinmdz.fmis.api.wrapper.AreaWrapper;
import com.jinmdz.fmis.common.util.DataUtil;
import com.jinmdz.fmis.core.base.BaseResult;
import com.jinmdz.fmis.core.model.OptionItem;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * AreaService类
 *
 * @author LiCongLu
 * @date 2020-01-16 17:27
 */
@Service("areaService")
public class AreaService extends BaseService {

    @Resource
    private AreaWrapper areaWrapper;

    /**
     * 加载省市县信息
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-01-19 10:45
     */
    public BaseResult<AreaOptionItem> loadProvinceCityCounty() {
        ArrayList<AreaOptionItem> loadItems = areaWrapper.listProvinceCityCounty();

        // 返回响应结果
        return successList(loadItems);
    }

    /**
     * 根据父主键查询子行政区划
     *
     * @param parentId 父主键
     * @return
     * @author LiCongLu
     * @date 2020-01-19 11:10
     */
    public BaseResult<OptionItem> loadAreaWithParentId(Integer parentId) {
        if (DataUtil.invalid(parentId)) {
            return failure("父主键错误");
        }

        ArrayList<OptionItem> loadItems = areaWrapper.listAreaByParentId(parentId, 0);
        return successList(loadItems);
    }

    /**
     * 按照主键加载子行政区划
     *
     * @param data 请求条件
     * @return
     * @author LiCongLu
     * @date 2020-05-21 17:03
     */
    public BaseResult<OptionItem> loadChildrenWithParentId(AreaParentData data) {
        if (DataUtil.invalid(data.getParentId())) {
            return failure("父主键错误");
        }

        ArrayList<OptionItem> loadItems = areaWrapper.listAreaByParentId(data.getParentId(), data.getAsProvinceCityCounty());
        return successList(loadItems);
    }
}
